<?php
//global_config::get_instance()->add_server_class("SoapAlphabetOfValueChar","soap_alphabet_value_char");
global_config::get_instance()->add_server_class("SoapAlphabetOfValueString","soap_alphabet_value_char");
require_once(dirname(__FILE__)."/soap_alphabet_base.class.php"); 
class soap_alphabet_value_char extends soap_alphabet_base 
{
	function __construct() {
		$this->Elements = new soap_array_of_value_char();
	}


	public function load_from_db($alphabet_id)
	{
		$this->Alphabet_id = $alphabet_id;
		$dba = db_adapter::get_instance();		
		$alphabet_message = new  alphabet_message(); 
		$criteria = new query_criteria();
		$criteria->add_criterion($alphabet_message->alphabet_id,criterion_type::$equal,$alphabet_id);
		$alphabet_list = $dba->get_list($alphabet_message,$criteria, $total, null, null, $alphabet_message->get_table_name().".".$alphabet_message->position->get_field_name(),"asc");
		while ($alphabet = $alphabet_list->get_next(false, true))
		{
    		$temp = new soap_value_char();
			$temp->load_from_db($alphabet->alphabet_message->message_id->get_value());
			$this->Elements->add($temp);
		}
	}

	/**
	 * Get id of alphabet contains this elements
	 *
	 * @param array $phantom_messages_ids
	 * @return int
	 */
	protected function get_alphabet_id_equals_this($messages_ids)
	{
		$dba = db_adapter::get_instance();
		$array_cross = array();
		$i = 0;
		do
		{
			$array_buf = array();

			$alphabet_message = new alphabet_message();

			$criteria = new query_criteria();
			$criteria->add_criterion($alphabet_message->message_id , criterion_type::$equal, $messages_ids[$i]);
			$criteria->add_criterion($alphabet_message->position, criterion_type::$equal,$i);
			$alphabet_message_list = $dba->get_list($alphabet_message,$criteria);

			while($item = $alphabet_message_list->get_next()) {
				$array_buf[]=$item->alphabet_id->get_value();
			}

			$array_cross = !empty($array_cross)? array_intersect($array_cross, $array_buf) : $array_cross = $array_buf;

			$i++;
		}while(!empty($array_cross) && $i<count($messages_ids));



		foreach ($array_cross as $alph_id) {

			$alphabet_cross = new alphabet_message();

			$criteria = new query_criteria();
			$criteria->add_criterion($alphabet_message->alphabet_id , criterion_type::$equal, $alph_id);
			$alphabet_cross_list = $dba->get_list($alphabet_cross,$criteria);

			if (count($messages_ids) == $alphabet_cross_list->count())
			{
				return $alph_id;
			}
		}
		return '';
	}


	/**
	 * Save messages to alphabet
	 *
	 * @param int $alphabet_id
	 * @param array $phantom_messages_ids
	 */
	protected function save_message_to_alphabet($alphabet_id , $messages_ids)
	{
		if (empty($alphabet_id))
		{
			throw new Exception("Try to save ". var_dump($messages_ids)."to - $alphabet_id - aphabet" );
		}
		$dba = db_adapter::get_instance();
		$pos = 0;
		foreach($messages_ids as $mess_id){
			$alphabet_message = new alphabet_message();
			$alphabet_message->alphabet_id->set_value($alphabet_id);
			$alphabet_message->message_id->set_value($mess_id);
			$alphabet_message->position->set_value($pos);
			$dba->save_unique($alphabet_message);
			$pos++;
		}
	}

    /**
     * Create new Alphabet in DB
     *
     * @return int created alphabet
     */
    protected function save_new_alphabet()
    {
        return parent::save_new_alphabet('char');
    }
}
?>
